﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.Entity;
using PDAMain.DAL;
using System.Runtime.InteropServices;

namespace PDAMain.Forms.ReadingWatermeter
{
    public partial class WaterReadingDetail : BaseForm
    {
        private int NowN;
        private int PreN;
        private string MPRId;
        private bool isTakePictrue = false;
        
        public WaterReadingDetail()
        {
            InitializeComponent();
            BindDate();
        }
        private void BindDate()
        {
            /*string WNum = Identity.WM_METER_ID;
            string strsql = " SELECT MPRId,WM_METER_ID,READMONTH,PRE_DEGREE,READDEGREE,DISTRICT_ID,"
                            + "BUILDING_ID,APARMENT_ID FROM WATERMETER WHERE WM_METER_ID='" + WNum + "'";
            DataSet ds = PublicMethods.SelectData(strsql);*/
            DataSet ds=DataHelper.ReadingWatermeterDao.getDetail(DataHelper.ReadingWatermeterDao.MprId);


            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                MPRId = ds.Tables[0].Rows[0]["MPRId"].ToString().Trim();
                lblWamterID.Text = ds.Tables[0].Rows[0]["WM_METER_ID"].ToString().Trim();
                lblpredegree.Text = ds.Tables[0].Rows[0]["PRE_DEGREE"].ToString().Trim();
                lbldistrict.Text = ds.Tables[0].Rows[0]["DISTRICT_ID"].ToString().Trim();
                lblbuilding.Text = ds.Tables[0].Rows[0]["BUILDING_ID"].ToString().Trim();
                lblapatment.Text = ds.Tables[0].Rows[0]["APARMENT_ID"].ToString().Trim();
                lblBarcode.Text = ds.Tables[0].Rows[0]["BARCODE"].ToString().Trim();
            }
        }

        private void menucomfirm_Click(object sender, EventArgs e)
        {
            //判断是数字类型
            string result= VerifyNumberData();
            if (result == "0") 
            {
                MessageBox.Show("请先输入本月读数！");
                return;
            }
            else if (result == "1")
            {
                MessageBox.Show("录入的本月读数不是有效地数据类型！");
                return;
            }
            else if (result == "2")
            {
                DialogResult dr;
                dr = MessageBox.Show("当前读数小于上月读数,是否继续？", "MessageBox", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (dr == DialogResult.Cancel)
                {
                    txtnownumber.Text = "";
                    return;
                }
            }
            if (!isTakePictrue)
            {
                MessageBox.Show("请先对水表读数进行拍照！");
                return;
            }

            saveData();


           /* if (bl)
            {
                //数据类型正确且拍照则保存数据
                if (isTakePictrue)
                {


                    string completedt = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss").Trim();
                    string strsql = " UPDATE WATERMETER SET READDEGREE='" + NowN + "',READINGTIME='"+completedt+"' WHERE WM_METER_ID='" + lblWamterID.Text + "'";
                    bool Flag = PublicMethods.InsertUpdateCreateDropData(strsql);

                    bool Flag = DataHelper.ReadingWatermeterDao.deal();




                    if (Flag == true)
                    {
                        try
                        {
                            pdaWebReference.PdaService pda = PublicMethods.getobject();
                            string degree = txtnownumber.Text.ToString();
                            bool blW= pda.updateWatermeterReadingWorkOrder(MPRId, degree, completedt, Identity.dutyPerson);

                            string delsql = " DELETE FROM WATERMETER WHERE WM_METER_ID='" + lblWamterID.Text + "'";
                            //更新成功刪除記錄
                            bool delFlag = PublicMethods.InsertUpdateCreateDropData(delsql);
                            WaterReadingList WRL = new WaterReadingList();
                            WRL.Show();
                            this.Close();
                        }
                        catch
                        {
                            MessageBox.Show("更新失败！");
                        }
                    }
                    else
                    {
                        MessageBox.Show("保存失败！");
                    }
                }
                else
                {
                    MessageBox.Show("请先拍照！");
                }
            }*/
        }

        private void saveData()
        {
            string degree = txtnownumber.Text.ToString().Trim();
            string completedt = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss").Trim();
           /* if (DataHelper.ReadingWatermeterDao.deal(DataHelper.ReadingWatermeterDao.MprId, degree, completedt, DataHelper.UserDao.getLoginedUserAccount()))
            {
                MessageBox.Show("成功同步数据至远程数据库");
            }
            else 
            {*/
                if (DataHelper.ReadingWatermeterDao.insertLacolTable(DataHelper.ReadingWatermeterDao.MprId, degree, completedt, DataHelper.UserDao.getLoginedUserAccount()))
                {
                    MessageBox.Show("保存数据成功");
                }
                else
                {
                    MessageBox.Show("保存数据失败");
                }
           // }
            backToList();
        }
        private void backToList()
        {
            WaterReadingList list = new WaterReadingList();
            list.Show();
            this.Close();
        }

        private void menuback_Click(object sender, EventArgs e)
        {
            backToList();
        }

        private string  VerifyNumberData()
        {
            string Flag = "-1";
            if (txtnownumber.Text.ToString().Trim() == "" || txtnownumber.Text.ToString().Trim() == null) 
            {
                Flag = "0";
                return Flag;
            }
            try
            {
              NowN = Convert.ToInt32(txtnownumber.Text.ToString().Trim());
            }
            catch
            {
             Flag = "1";
             return Flag;

            }
            if (lblpredegree.Text.ToString().Trim() != null || lblpredegree.Text.ToString().Trim() != "")
            {
                PreN = Convert.ToInt32(lblpredegree.Text.ToString().Trim());
            }
            else
            {
                PreN = 0;
            }
            if (PreN > NowN)
            {

                /* DialogResult dr;
                 dr = MessageBox.Show("当前读数小于上月读数,是否继续？", "MessageBox", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                 if (dr == DialogResult.OK)
                 {
                     Flag = true;
                 }
                 else
                 {
                     txtnownumber.Text = "";
                     Flag = false;
                 }*/
                Flag = "2";
            }
            return Flag;
        }

        private void btnPicture_Click(object sender, EventArgs e)
        {
            try
            {
                TakePicture TakeP = new TakePicture();
                string WMeterID = lblWamterID.Text.ToString();
                string folderName = "Watermeter";
                //string strFileName = WMeterID;
                string strFileName = DataHelper.ReadingWatermeterDao.MprId;
                Bitmap bp = TakeP.Show(folderName, strFileName);
                if (bp != null)
                {
                    isTakePictrue = true;
                    PictureL.Image = bp;
                }
                TakeP.dispose();
            }
            catch
            {
                MessageBox.Show("点击拍照失败，请重新操作");
            }
        }
        
    }
}